package com.yiwise.service.impl;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import com.yiwise.service.PhoneConvertService;
import org.springframework.stereotype.Service;

/**
 * AES加解密
 * Created by 昌夜 on 2022/11/23.
 */
@Service("aesConvertService")
public class AesConvertServiceImpl implements PhoneConvertService {
    // 秘钥
    private String AES_KEY = "0123456789ABCDEF0123456789ABCDEF";

    @Override
    public String encrypt(String originNumber) {
        AES aes = SecureUtil.aes(AES_KEY.getBytes());
        String content = aes.encryptHex(originNumber);
        return content;
    }

    @Override
    public String decrypt(String encryptedNumber) {
        AES aes = SecureUtil.aes(AES_KEY.getBytes());
        String content = aes.decryptStr(encryptedNumber, CharsetUtil.CHARSET_UTF_8);
        return content;
    }

    public static void main(String[] args) {
        PhoneConvertService conv = new AesConvertServiceImpl();
        String a = "13088876543";
        String b = conv.encrypt(a);
        String c = conv.decrypt(b);
        System.out.println(b + "<---->" + c);

        String a1 = "1799";
        String b1 = conv.encrypt(a1);
        String c1 = conv.decrypt(b1);
        System.out.println(b1 + "<---->" + c1);
    }
}
